﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hired
{
    public static class DBBuddyRequestRoster
    {
        //create buddy request
        public static bool addBuddyRequest(BuddyRequestRoster brr)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.AddToBuddyRequestRosters(brr);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //retrieve buddy request by both sender and recipient IDs
        public static BuddyRequestRoster retrieveBuddyRequest(string senderID, string recipientID)
        {
            BuddyRequestRoster buddyRequest = null;
            using (hireddbEntities db = new hireddbEntities())
            {
                buddyRequest =
                (from a in db.BuddyRequestRosters
                 where a.SenderID == senderID && a.RecipientID == recipientID
                 select a).SingleOrDefault();
            }

            return buddyRequest;
        }

        //retrieve all buddy requests in a list for a recipient account
        public static List<BuddyRequestRoster> retrieveBuddyRequestListByRecipient(string recipientID)
        {
            List<BuddyRequestRoster> buddyRequestList = null;
            using (hireddbEntities db = new hireddbEntities())
            {
                buddyRequestList =
                (from a in db.BuddyRequestRosters
                 where a.RecipientID == recipientID
                 select a).ToList();
            }
            return buddyRequestList;
        }

        //retrieve all buddy requests in a list for a sender account
        public static List<BuddyRequestRoster> retrieveBuddyRequestListBySender(string senderID)
        {
            List<BuddyRequestRoster> buddyRequestList = null;
            using (hireddbEntities db = new hireddbEntities())
            {
                buddyRequestList =
                (from a in db.BuddyRequestRosters
                 where a.SenderID == senderID
                 select a).ToList();
            }
            return buddyRequestList;
        }

        public static List<Buddy> retrieveBuddyRequestListForDisplay(string recipientID)
        {
            List<BuddyRequestRoster> buddyRequestList = retrieveBuddyRequestListByRecipient(recipientID);
            List<string> nameList = new List<string>();
            List<string> imageUrlList = new List<string>();
            List<string> usernameList = new List<string>();
            List<string> idList = new List<string>();

            var query =
                from br in buddyRequestList
                where br.RecipientID == recipientID
                select br;

            #region Retrieving values for each buddy
            foreach (BuddyRequestRoster br in buddyRequestList)
            {
                string senderID = br.SenderID;

                using (hireddbEntities db = new hireddbEntities())
                {
                    switch (DBAccount.RetrieveAccountTypeByID(senderID))
                    {
                        case "CompanyAccount":
                            var ca =
                                (from c in db.CompanyAccounts.Include("Company")
                                 where c.CAccountID == senderID
                                 select c).SingleOrDefault();
                            nameList.Add(ca.Company.CompanyName);
                            imageUrlList.Add(ca.Company.ImageAddress);
                            usernameList.Add(ca.Username);
                            idList.Add(ca.CAccountID);
                            break;
                        case "JobseekerAccount":
                            var jsa =
                                (from j in db.JobseekerAccounts.Include("Jobseeker")
                                 where j.JAccountID == senderID
                                 select j).SingleOrDefault();
                            nameList.Add(jsa.Jobseeker.FirstName + " " + jsa.Jobseeker.LastName);
                            imageUrlList.Add(jsa.Jobseeker.ImageAddress);
                            usernameList.Add(jsa.Username);
                            idList.Add(jsa.JAccountID);
                            break;
                    }
                }
            }
            #endregion

            List<Buddy> listToReturn = new List<Buddy>();
            for (int i = 0; i < buddyRequestList.Count; i++)
            {
                Buddy b = new Buddy();
                b.UserID = idList[i];
                b.ImageAddress = imageUrlList[i];
                b.Name = nameList[i];
                b.Username = usernameList[i];
                listToReturn.Add(b);
            }

            return listToReturn;
        }

        //delete buddy requests by both sender and recipient IDs
        public static bool deleteBuddyRequest(string senderID, string recipientID)
        {
            try
            {
                BuddyRequestRoster buddyRequestToDelete = retrieveBuddyRequest(senderID, recipientID);
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Attach(buddyRequestToDelete);
                    db.DeleteObject(buddyRequestToDelete);
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        //check if buddy relationship has been requested between two accounts, either way.
        public static bool checkBuddyRequested(string senderID, string recipientID)
        {
            try
            {
                List<BuddyRequestRoster> buddyRequestList = retrieveBuddyRequestListBySender(senderID);

                foreach (BuddyRequestRoster br in buddyRequestList)
                {
                    if (br.SenderID == senderID && br.RecipientID == recipientID)
                    {
                        return false;
                    }
                    else if (br.SenderID == recipientID && br.RecipientID == senderID)
                    {
                        return false;
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
       
    }
}